// 绫诲悕鎿嶄綔................................................................................................

// 鏌ユ壘绫诲悕鍏冪礌
function getElementsByClass(node, className) {
    if (node.getElementsByClassName) {
        return node.getElementsByClassName(className);
    } else {
        var results = new Array(),
            elements = node.getElementsByTagName('*');
        forEach(elements, function (element, index) {
            if (element.className.indexOf(className) != -1) {
                results[results.length] = element;
            }
        });
    }
}

// 绫诲悕鏂板
function addClass(element, className) {
    if (element.classList) {
        element.classList.add(className);
    } else {
        element.className += ' ' + className;
    }

    return element;
}

// 绫诲悕鍒犻櫎
function removeClass(element, className) {
    if (element.classList) {
        element.classList.remove(className);
    } else {
        element.className = element.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)',
            'gi'), ' ');
    }

    return element;
}

// 绫诲悕鍖呭惈
function hasClass(element, className) {
    if (element.classList) {
        return element.classList.contains(className);
    } else {
        return new RegExp('(^| )' + className + '( |$)', 'gi').test(element.className);
    }
}

// 鏁扮粍鎿嶄綔.........................................................................................................

// 閬嶅巻鏁扮粍
function forEach(elements, callback) {
    if ([].forEach) {
	        [].forEach.call(elements, callback);
    } else {
        for (var i = 0; i < elements.length; i++) {
            callback(elements[i], i);
        }
    }
}

// 鍏勫紵鍏冪礌..........................................................................................................

// 鎵€鏈夊叾浠栧厔寮熷厓绱犳暟缁勶紝浠ュ強褰撳墠绱㈠紩
function siblings(obj) {
    var self = obj,
        arr = [],
        index,
        p = self.parentNode.children;

    for (var i = 0, pl = p.length; i < pl; i++) {
        if (p[i] !== self) {
            arr.push(p[i]);
        } else {
            index = i;
        }
    }

    return {
        arr: arr,
        index: index
    };
}

// 閬嶅巻鑷虫寚瀹氱埗鍏冪礌
function findParent(obj, target) {
    while (obj !== target) {

        if (obj == null) {
            return false;
        }

        obj = obj.parentNode;
    }

    return obj;
}

//ele鏄唴閮ㄥ厓绱狅紝target鏄綘鎯虫壘鍒扮殑鍖呰９鍏冪礌
function getTargetNode(ele, target) {
    if (!ele || ele === document) return false;
    return ele === target ? true : getTargetNode(ele.parentNode, target);
}

// Tab鍒囨崲
function tab(element, className) {
    // 鑷韩娣诲姞绫诲悕
    addClass(element, className);
    // 鍏勫紵鍏冪礌绉婚櫎绫诲悕
    siblings(element).arr.forEach(function (sibling) {
        removeClass(sibling, className);
    });
}

// 浜嬩欢鎿嶄綔..........................................................................................................

// 缁戝畾浜嬩欢
function addEvent(element, type, handle, capture) {
    if (element.addEventListener) {
        element.addEventListener(type, handle, capture);
    } else if (element.attachEvent) {
        element.attachEvent('on' + type, handle);
    } else {
        element['on' + type] = handle;
    }
}

// 绉婚櫎浜嬩欢
function removeEvent(element, type, handle, capture) {
    if (element.removeEventListener) {
        element.removeEventListener(element, type, handle, capture);
    } else if (element.detachEvent) {
        element.detachEvent('on' + type, handle);
    } else {
        element['on' + type] = null;
    }
}

// 浜嬩欢浠ｇ悊
function eventBroker(event, className, fn) {
    var event = event || window.event,
        target = event.target;

    while (target) {
        if (target && target.nodeName == '#document') {
            break;
        } else if (hasClass(target, className)) {
            fn();
            break;
        }

        target = target.parentNode;
    }
}

// 瑙﹀彂浜嬩欢
function trigger(element, type) {
    if (document.createEvent) {
        var event = document.createEvent('HTMLEvents');
        event.initEvent(type, true, false);
        element.dispatchEvent(event);
    } else {
        element.fireEvent('on' + type);
    }
}

// 鑾峰彇鍥剧墖鐨勫師濮嬪昂瀵�.........................................................................................................

function getImgNaturalDimensions(img) {
    var nWidth, nHeight;

    if (img.naturalWidth) { // 鐜颁唬娴忚鍣�
        nWidth = img.naturalWidth;
        nHeight = img.naturalHeight;
    } else { // IE6/7/8
        var imgae = new Image();

        image.src = img.src;
        image.onload = function () {
            nWidth = image.width;
            nHeight = image.height;

            return [nWidth, nHeight];
        }
    }

    return [nWidth, nHeight];
}

function formatNum(str) {
    var newStr = "",
        count = 0;

    // 闈炲皬鏁扮殑鎯呭喌
    if (str.indexOf(".") == -1) {

        for (var i = str.length - 1; i >= 0; i--) {
            // 绗�4,7,10浣嶇殑鎯呭喌
            if (count % 3 == 0 && count != 0) {
                newStr = str.charAt(i) + "," + newStr;
            } else {
                newStr = str.charAt(i) + newStr;
            }

            count++;
        }
        //鑷姩琛ラ綈涓や綅灏忔暟
        // str = newStr + ".00";
        return newStr;
    } else {
        for (var i = str.indexOf(".") - 1; i > 0; i--) {
            if (count % 3 == 0 && count != 0) {
                newStr = str.charAt(i) + "," + newStr;
            } else {
                newStr = str.charAt(i) + newStr;
            }

            count++;
        }

        str = newStr + (str + "00").substr((str + "00").indexOf("."), 3);
    }
}


// Scroll浜嬩欢................................................................................

// 杩斿洖椤堕儴
function topBack() {
    var timer = null;

    timer = setInterval(function () {
        var backTop = document.body.scrollTop;
        var speed = backTop / 5;
        document.body.scrollTop = backTop - speed;

        if (backTop == 0) {
            clearInterval(timer);
        }
    }, 30);
}